
package com.lagou.controller;

import com.lagou.pojo.AuthCode;
import com.lagou.pojo.Token;
import com.lagou.service.CodeService;
import com.lagou.service.TokenService;
import com.lagou.util.ResponseData;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@RestController
@RequestMapping("/user")
public class UserController {



    @Reference
    private CodeService codeService;

    @Autowired
    private TokenService tokenService;

    @GetMapping
    public String test(){
        return "success";
    }

    @PostMapping("/register/{email}/{password}/{code}")
    public ResponseData register(@PathVariable(name = "email") String email,
                                 @PathVariable(name = "password")String password,
                                 @PathVariable(name = "code")String code,
                                 HttpServletResponse response,
                                 HttpServletRequest request) throws IOException {
        Date date = new Date();
        AuthCode authCode = codeService.getAuthCodeByEmailAndCode(email, code);
        if (authCode == null){
            return ResponseData.fail("验证码错误");
        }

        Date expiretime = authCode.getExpiretime();
        if (expiretime.compareTo(date)<0){
            ResponseData responseData = ResponseData.fail("验证码已过期");
            return responseData;
        }


        String token = tokenService.genToken(email, password);
        Cookie cookie = new Cookie("user-cookie", token);
        cookie.setPath("/");
        //设置Cookie的最大生命周期,否则浏览器关闭后Cookie即失效
        cookie.setMaxAge(Integer.MAX_VALUE);
        System.out.println("保存cookie："+token);
        //将Cookie加到response中
        response.addCookie(cookie);
        return ResponseData.success("验证成功");
    }

    @GetMapping("/getEmailByToken")
    public ResponseData getEmailByToken(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        String token = "";
        if (cookies!=null){
            for (Cookie cookie : cookies) {
                if ("user-cookie".equals(cookie.getName())){
                    token = cookie.getValue();
                }
            }
        }
        Token userToken = tokenService.getByToken(token);
        if (userToken==null){
            return ResponseData.fail("未查询到email");
        }
        return ResponseData.success("",userToken);
    }

    /**
     * 登录
     */
    @GetMapping("/login/{email}/{password}")
    public ResponseData login(@PathVariable(name = "email")String email,@PathVariable(name = "password")String password,HttpServletResponse response){
        String token = tokenService.genToken(email, password);
        Cookie cookie = new Cookie("user-cookie", token);
        cookie.setPath("/");
        //设置Cookie的最大生命周期,否则浏览器关闭后Cookie即失效
        cookie.setMaxAge(Integer.MAX_VALUE);
        System.out.println("保存cookie："+token);
        //将Cookie加到response中
        response.addCookie(cookie);
        return ResponseData.success("登录成功");
    }

    /**
     * 根据token获取email
     */
    @GetMapping("/getEmail")
    public ResponseData getEmailByToken(String token){
        Token userToken = tokenService.getByToken(token);
        if (userToken==null){
            return ResponseData.fail("未查询到email");
        }
        return ResponseData.success("",userToken);
    }
}